import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { defualtRoute } from '@/utils/map-menu'
import cache from '@/utils/cache'
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/main'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/login.vue')
  },
  {
    path: '/main',
    name: 'Main',
    component: () => import('@/views/main/main.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/not-found/404.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to) => {
  const token = cache.getCache('cms-token')
  if (to.name !== 'Login') {
    if (!token) {
      return '/login'
    }
  }
  if (to.name == 'Main') {
    return defualtRoute.path
  }
})
export default router
